//index.js
//获取应用实例
const app = getApp()

const utils = require("../../utils/util.js");

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    count: 1,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  add: function (event) {
    console.log(event);
    console.log(this);
    this.setData({ count: this.data.count + 1 })
    wx.showToast({
      title: '成功',
      icon: 'success',
      duration: 2000
    });
    // wx.showLoading({
    //   title: '加载中',
    // })
    wx.showActionSheet({
      itemList: ['A', 'B', 'C'],
      success: function (res) {
        console.log(res.tapIndex)
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })

  },
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    });
  },
  onLoad: function () {
    wx.setStorage({
      key: "token",
      data: "Vi9pVTZTZm1iSGpGRWo0dzd6TWJxUT09OmE2d29ER2J3dCtFemlxRzNKcFhVT0E9PQ"
    });

   
    
    this.wxLogin({});
    
    
  },

  // 获取用户info
  getUserInfo: function (e) {
    utils.get('/qbmf/api/user',{}).then((res)=>{
      
    },(err)=>{});
  },

  // 查询授权
  _init(){
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo);
              this.wxLogin(res.userInfo);
            }
          })
        } else {
          // 授权
          wx.authorize({
            scope: 'scope.userInfo',
            success: function (res) {
              console.log(res);
              this.wxLogin(res.userInfo);
            }
          });
        }
      }
    });
  },

  // 判断微信用户是否登录
  wxLogin(userInfo){

    // 用户是否登录
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {

          /*
          //发起网络请求sever
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
              "appid": app.globalData.appid,
              "secret": app.globalData.AppSecret,
              "js_code": res.code,
              "grant_type": res.code
            },
            success:function(res){
              console.log(res);


            }
          })*/
        } else {

        }
      }
    });
  }





})
